<?php

class account{
	
	// Define variables
	private $ID_ACCOUNT;
	private $CLIENT_ID;
	private $ACCOUNT_NUM;
	private $CURRENCY_ID;
	private $BALANCE;
	
	// Connection with database and instance creation
	private $DB;
	public function __construct() {
		$config = config::getInstance();
		$this->DB = $config->config_values['database']['db_prefix'];
	}
	
	// Getters and setters functions
  	public function setID_ACCOUNT($value) {
  		$this->ID_ACCOUNT = $value;
  	}
	public function getID_ACCOUNT() {
  		return $this->ID_ACCOUNT;
  	}
  	public function setCLIENT_ID($value) {
  		$this->CLIENT_ID = $value;
  	}
	public function getCLIENT_ID() {
  		return $this->CLIENT_ID;
  	}
	public function setACCOUNT_NUM($value) {
  		$this->ACCOUNT_NUM = $value;
  	}
	public function getACCOUNT_NUM() {
  		return $this->ACCOUNT_NUM;
  	}
	public function setCURRENCY_ID($value) {
  		$this->CURRENCY_ID = $value;
  	}
	public function getCURRENCY_ID() {
  		return $this->CURRENCY_ID;
  	}
	public function setBALANCE($value) {
  		$this->BALANCE = $value;
  	}
	public function getBALANCE() {
  		return $this->BALANCE;
  	}

	// General functions:
  	public function load($id)
	{
		$db = db::getInstance();
		$sql = "SELECT * FROM ".$this->DB."account WHERE ID_ACCOUNT=".$id;

		foreach ($db->query($sql) as $row) {
			$this->setID_ACCOUNT($row['ID_ACCOUNT']);
			$this->setCLIENT_ID($row['CLIENT_ID']);
			$this->setACCOUNT_NUM($row['ACCOUNT_NUM']);
			$this->setCURRENCY_ID($row['CURRENCY_ID']);
			$this->setBALANCE($row['BALANCE']);
		}
	    $db = null; 
	    return true;			
  	}

  	public function insert(){}

  	public function modify($id){
  		try {
	  		$db = db::getInstance();
	  		$sql = "UPDATE ".$this->DB."account SET ";
	  		$sql .=" CLIENT_ID = ".$this->getCLIENT_ID();
	  		$sql .=",ACCOUNT_NUM = '".$this->getACCOUNT_NUM()."'";
	  		$sql .=",CURRENCY_ID = ".$this->getCURRENCY_ID();
	  		$sql .=",BALANCE = ".$this->getBALANCE();
	  		$sql .=" WHERE ID_ACCOUNT=" . $id;
	    	$sth=$db->prepare($sql);
			$sth->execute();
			$db = null;
			return true;
  		} catch (Exception $e) {
  			return false;
  		}
  	}

  	public function delete($id){}
  	
  	// JSON function:
	public function jsonList($id, $currency){
		try {
    		$db = db::getInstance();
			$sql = "SELECT * FROM ".$this->DB."account ";
			$sql.= "JOIN ".$this->DB."currency ON ID_CURRENCY = CURRENCY_ID ";
			if(empty($currency)){
				$sql.= "WHERE CLIENT_ID = ".$id;
			}else{
				$sql.= "WHERE CURRENCY_ID = ".$currency." AND CLIENT_ID != ".$id;
			}
			$sth=$db->prepare($sql);
			$sth->execute();
			$recordset = array();
			$recordset = $sth->fetchAll(PDO::FETCH_ASSOC);
			return $recordset;			
		} catch (Exception $e) {
  			return false;
		}		
	}
  	
}
?>